using System;
using System.Xml;

namespace stunnware.CRM.Fetch {
   /// <summary>
   /// Helper class to convert a FetchStatement to a FetchXML string.
   /// </summary>
   internal class FetchXmlDocument {
 
      XmlDocument _doc;
      XmlElement _root;

      public FetchXmlDocument() {
         _doc = new XmlDocument();
         _root = _doc.CreateElement("fetch");
         _doc.AppendChild(_root);
      }

      internal void AddRootAttribute(string name, object value) {
         AddAttribute(_root, name, value);
      }

      internal static void AddAttribute(XmlNode parent, string name, object value) {
         XmlAttribute attr = parent.OwnerDocument.CreateAttribute(name);
         attr.Value = value.ToString();
         parent.Attributes.Append(attr);
      }

      internal XmlElement AddRootElement(string name) {
         return AddElement(_root, name);
      }

      internal static XmlElement AddElement(XmlElement parentElement, string name) {
         XmlElement element = parentElement.OwnerDocument.CreateElement(name);
         parentElement.AppendChild(element);
         return element;
      }

      public override string ToString() {
         return _doc.OuterXml;
      }
   }
}